package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.PackingListDao;
import cn.itcast.dao.cargo.ShippingOrderDao;
import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private ShippingOrderDao shippingOrderDao;

    @Autowired
    private PackingListDao packingListDao;

    @Override
    public void save(Invoice invoice) {
        invoice.setStatus(0);
        invoiceDao.insert(invoice);
        ShippingOrder shippingOrder = shippingOrderDao.selectById(invoice.getInvoiceId());
        shippingOrder.setState(1);
        shippingOrderDao.updateById(shippingOrder);
    }

    @Override
    public PageInfo<Invoice> findByPage(Integer pageNum, Integer pageSize, String loginUserCompanyId) {
        PageHelper.startPage(pageNum,pageSize);

        List<Invoice> invoiceList = invoiceDao.selectList(new QueryWrapper<Invoice>().eq("company_id", loginUserCompanyId));

        PageInfo<Invoice> pageInfo = new PageInfo<>(invoiceList);

        return pageInfo;
    }

    @Override
    public ResultInfo delete(String invoiceId) {
        int count = invoiceDao.delete(new QueryWrapper<Invoice>().eq("invoice_id", invoiceId).ne("status", 1));
        if (count==1){
            ShippingOrder shippingOrder = shippingOrderDao.selectById(invoiceId);
            shippingOrder.setState(0);
            shippingOrderDao.updateById(shippingOrder);
            return new ResultInfo(true);
        }else {
            return new ResultInfo(false,"删除失败，请检查发票状态！");
        }
    }


    /*
        需要：
        1.finance预处理信息
            1.1发票金额invoice_money选择发票时候从发票中自动带过来
               发票id
            1.2报运合同号export_nos数据来源于装箱表(装箱表)
            1.3装运港shipment_port数据来源于报运单表(装箱表)
            1.4目的港destination_port数据来源于报运单表(装箱表)
            1.5收货人consignee数据来源于报运单表(装箱表)
            需要：
            发票信息、装箱信息
     */
    @Override
    public ResultInfo finance(String invoiceId) {
        //创建finance
        Finance finance = new Finance();
//        finance.setFinanceId(invoiceId);
        finance.setInvoiceId(invoiceId);

        //查询发票信息
        Invoice invoice = invoiceDao.selectById(invoiceId);

        if (invoice==null) return new ResultInfo(false,"发票不存在！");

        //获取发票金额invoice_money
        finance.setInvoiceMoney(invoice.getInvoiceMoney());

        //获取发票时间
        finance.setInvoiceTime(invoice.getInvoiceTime());

        //查询装箱信息
        PackingList packingList = packingListDao.selectById(invoiceId);

        //获取
        //报运合同号export_nos数据来源于装箱表(装箱表)
        finance.setExportNos(packingList.getExportNos());

        //装运港shipment_port数据来源于报运单表(装箱表)
        finance.setShipmentPort(packingList.getShipmentPort());

        //目的港destination_port数据来源于报运单表(装箱表)
        finance.setDestinationPort(packingList.getDestinationPort());

        //收货人consignee数据来源于报运单表(装箱表)
        finance.setConsignee(packingList.getConsignee());

        //返回数据
        return new ResultInfo(true,"",finance);
    }

    @Override
    public Invoice findById(String id) {
        return invoiceDao.selectById(id);
    }


}
